<template>
	<view class="container">
		<view class="page-title">
			<text>{{ custName }}</text>
		</view>

		<view class="search-results-container">
			<view v-for="(ecCustAddress, index) in addressList" :key="index">
				<uni-row>
					<uni-col :span="14">
						<view class="search-result">
							<uni-row>
								<uni-col :span="14">
									<view>
										<text>{{ ecCustAddress.contactsName }}</text>
									</view>
								</uni-col>
								<uni-col :span="10">
									<view>
										<text>{{ ecCustAddress.contactsPhone }}</text>
									</view>
								</uni-col>
							</uni-row>
						</view>
						<view class="search-result-address">
							<text>地址：{{ ecCustAddress.goodsDetailedAddress }}</text>
						</view>
					</uni-col>
					<uni-col :span="10">
						<view class="search-result-address">
							<view></view>
							<button class="delete-button" @tap="goToDetailChangePage(ecCustAddress.pkid)">编辑</button>
							<button class="delete-button" @click="handleDelete(ecCustAddress)">删除</button>
						</view>
					</uni-col>
				</uni-row>
			</view>
		</view>

		<view style="position: fixed; bottom: 0px; width: 100%;">
			<uni-row>
				<uni-col :span="12">
					<view>
						<button @click="goToHome">返回首页</button>
					</view>
				</uni-col>
				<uni-col :span="12">
					<view>
						<button @tap="goToDetailAddPage()">新增客户地址</button>
					</view>
				</uni-col>
			</uni-row>
		</view>
	</view>
</template>

<script>
	import {
		listAddress,
		delAddress,
	} from '@/api/tms/address'

	export default {
		mounted() {
			const custName = this.$route.query.custName;
			if (custName) {
				this.custName = custName;
				this.openAddressDetails(custName);
			}
		},
		data() {
			return {
				addressList: [],
				custName: "",
				ecCustAddress: {
					custName: "",
					contactsPhone: "",
					goodsDetailedAddress: ""
				}
			};
		},
		methods: {
			goToHome() {
				uni.switchTab({
					url: '/pages/work/index',
				});
			},
			openAddressDetails(custName) {
				listAddress({
					"custName": custName
				}).then(response => {
					this.addressList = response.rows;
				});
			},
			goToDetailChangePage(pkid) {
				uni.navigateTo({
					url: '/pages/ec/addressChange/index?pkid=' + pkid + "&custName=" + this.custName,
				});
			},
			goToDetailAddPage() {
				uni.navigateTo({
					url: '/pages/ec/addressDetailAdd/index?custName=' + this.custName,
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const pkids = row.pkid || this.ids;
				this.$modal.confirm('是否确认删除客户地址管理编号为"' + pkids + '"的数据项？').then(function() {
					return delAddress(pkids);
				}).then(() => {
					this.openAddressDetails(this.custName);
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
		},
	};
</script>

<style>
	.container {
		dispaly: flex;
		flex-direction: row;
		flex: auto;
		background-color: #fff
	}

	.page-title {
		background: #3e7baf;
		color: #fff;
		text-align: center;
		font-size: 1.3rem;
		font-weight: "300";
	}

	.search-results-container {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 200%;
		padding: 3px;
		background-color: #fff;
		padding-bottom: 80px;
	}

	.search-result {
		flex: 1;
		margin-bottom: 10px;
		background-color: #fff;
		padding: 7px;
		border-bottom: none;
		font-weight: bold;
	}

	.search-result-address {
		font-size: 12px;
		flex: 1;
		margin-bottom: 10px;
		background-color: #fff;
		padding: 7px;
		display: flex;
		align-items: center;
	}

	.delete-button {
		font-size: 12px;
		border: 1px solid orange;
		border-radius: 4px;
		color: orange;
		margin-right: 0px;
	}
</style>